package com.shop2cn.iapi.coupon.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.parameter.getUserValidSellerCouponsBean;
import com.shop2cn.iapi.coupon.parameter.GetUserValidSellerCouponsResp.CouponDto;
import com.shop2cn.iapi.coupon.service.UserReceiveCouponCall;
import com.shop2cn.iapi.coupon.service.getUserValidSellerCouponsCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.CouponShop2cn.coupon;
import com.ymttest.database.model.CouponShop2cn.couponbatch;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取用户已领买手有效的优惠券列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getUserValidSellerCoupons{
	private static getUserValidSellerCouponsBean bean; 
	private static getUserValidSellerCouponsCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户已领买手有效的优惠券列表");
	}

	@Before
	public void caseUp() {
		bean=new getUserValidSellerCouponsBean();
		call=new getUserValidSellerCouponsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserValidSellerCoupons_001() {
		Logger.start(true,"获取用户已领买手有效的优惠券列表-有门槛");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponMCallService.setDefaultSellerCreateBatchBean();
			
			SellerCreateBatchCall sellerCreateBatchCall = couponMCallService.SellerCreateBatch(sellerCreateBatchBean);

			UserReceiveCouponCall userReceiveCouponCall=couponMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);//领取优惠券
			
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
			verifyReturn(sellerCreateBatchCall.getBatchCode(),userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserValidSellerCoupons_002() {
		Logger.start(true,"获取用户已领买手有效的优惠券列表-无门槛");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponMCallService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean.setIsShowInPage(false);
			SellerCreateBatchCall sellerCreateBatchCall = couponMCallService.SellerCreateBatch(sellerCreateBatchBean);

			UserReceiveCouponCall userReceiveCouponCall=couponMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);//领取优惠券
			
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
			verifyReturn(sellerCreateBatchCall.getBatchCode(),userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserValidSellerCoupons_003() {
		Logger.start(true,"获取用户已领买手有效的优惠券列表-SellerId=0返回平台优惠券");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();

			// 平台优惠券-链接
			CreateCouponBean createcouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();	
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createcouponBean);
			UserReceiveCouponCall userReceiveCouponCall=couponCallService.receiveCouponInfo(createCouponCall.getBatchCode(),userId);//领取优惠券
			
			bean.setSellerId(0);
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
		//	verifyReturn(createCouponCall.getBatchCode(),userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserValidSellerCoupons_004() {
		Logger.start(true,"获取用户已领买手有效的优惠券列表-仅直播使用优惠券-JIRACONN-26114");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponMCallService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean.setCouponType((byte)4);
			SellerCreateBatchCall sellerCreateBatchCall = couponMCallService.SellerCreateBatch(sellerCreateBatchBean);

			UserReceiveCouponCall userReceiveCouponCall=couponMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);//领取优惠券
			
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
			verifyReturn(sellerCreateBatchCall.getBatchCode(),userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn(String batchCode,String couponCode)
	{
	    List<CouponDto> Couponlst=call.getSellerCouponsList().getCouponDtos();
	    List<couponbatch> couponbatchs = new couponbatchWapper().selectBybatchCode(batchCode);
	    
	    Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
		Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
		
	    Logger.verifyEquals(couponCode, Couponlst.get(0).getCouponCode(), "验证数据库coupon表中的字段CouponCode与返回的CouponCode是否一致");
	    Logger.verifyEquals(Couponlst.size(), couponbatchs.size(), "验证返回数据库的数量与返回的数量一致");
	    for(int i=0;i<Couponlst.size();i++)
	    {
	    	List<coupon> couponlst = new couponWapper().selectByBatchId(couponbatchs.get(i).getBatchId());
	    	 
	    	Logger.verifyEquals(Couponlst.get(i).getBatchId(), couponbatchs.get(i).getBatchId(), "验证数据库couponbatch表中的字段batchId与返回的batchId是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).getBatchCode(), couponbatchs.get(i).getBatchCode(), "验证数据库couponbatch表中的字段BatchCode与返回的BatchCode是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).isNoThreshold(), couponbatchs.get(i).getIsShowInPage(), "验证数据库couponbatch表中的字段NoThreshold与返回的IsShowInPage是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).getCouponAmount().stripTrailingZeros(), couponbatchs.get(i).getCouponValue().stripTrailingZeros(), "验证数据库couponbatch表中的字段CouponAmount与返回的CouponValue是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).getOrderAmount().stripTrailingZeros(), couponbatchs.get(i).getMinOrderValue().stripTrailingZeros(), "验证数据库couponbatch表中的字段OrderAmount与返回的MinOrderValue是否一致");
	    	
	    	Logger.verifyEquals(Couponlst.get(i).getBatchName(), couponbatchs.get(i).getCouponName(), "验证数据库couponbatch表中的字段CouponName与返回的BatchName是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).getValidStart(), couponlst.get(i).getValidStart(), "验证数据库coupon表中的字段ValidStart与返回的ValidStart是否一致");
	    	Logger.verifyEquals(Couponlst.get(i).getValidEnd(), couponlst.get(i).getValidEnd(), "验证数据库coupon表中的字段ValidEnd与返回的ValidEnd是否一致");
	    	
	    	Logger.verifyEquals(Couponlst.get(i).getCouponType(), couponbatchs.get(i).getCouponType(), "验证数据库couponbatch表中的字段CouponType与返回的CouponType是否一致");
	    } 
	}
}